<template>
  <div class="backpack-page">
    <!-- Header -->
    <div class="backpack-header">
      <button class="back-button" @click="goHome">
        <span class="back-icon">←</span>
      </button>
      <h1 class="header-title">我的背包</h1>
    </div>

    <!-- Content -->
    <div class="backpack-content">
      <div class="backpack-card">
        <div class="tabs">
          <button
              v-for="tab in tabs"
              :key="tab.id"
              class="tab-button"
              :class="{ active: activeTab === tab.id }"
              @click="activeTab = tab.id"
          >
            {{ tab.name }}
          </button>
        </div>

        <div class="tab-content">
          <!-- Pokémon Tab -->
          <div v-if="activeTab === 'pokemon'" class="pokemon-grid">
            <div
                v-for="pokemon in myPokemon"
                :key="pokemon.id"
                class="pokemon-card"
                :class="{ selected: selectedPokemon?.id === pokemon.id }"
                @click="selectPokemon(pokemon)"
            >
              <div class="pokemon-card-content">
                <img
                    :src="`https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${pokemon.id}.png`"
                    :alt="pokemon.name"
                    class="pokemon-thumbnail"
                />
                <h3 class="pokemon-card-name">{{ pokemon.name }}</h3>
                <div class="type-badges">
                  <span
                      v-for="(type, index) in pokemon.type.split('/')"
                      :key="index"
                      class="type-badge"
                      :style="{ backgroundColor: getTypeColor(type) }"
                  >
                    {{ type }}
                  </span>
                </div>
                <p class="pokemon-level">Lv. {{ pokemon.level }}</p>
              </div>
            </div>
          </div>

          <!-- Items Tab -->
          <div v-else-if="activeTab === 'items'" class="items-grid">
            <div class="item-card">
              <img
                  src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/items/poke-ball.png"
                  alt="精灵球"
                  class="item-image"
              />
              <p class="item-name">精灵球</p>
              <p class="item-quantity">x{{ items.pokeBall }}</p>
            </div>
            <div class="item-card">
              <img
                  src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/items/potion.png"
                  alt="药水"
                  class="item-image"
              />
              <p class="item-name">药水</p>
              <p class="item-quantity">x{{ items.potion }}</p>
            </div>
          </div>

          <!-- Badges Tab -->
          <div v-else-if="activeTab === 'badges'" class="empty-tab">
            <div class="info-icon">i</div>
            <p>尚未获得任何徽章</p>
          </div>

          <!-- Pokémon Details -->
          <PokemonCard
              v-if="selectedPokemon && activeTab === 'pokemon'"
              :pokemon="selectedPokemon"
              :type-color-map="typeColorMap"
              :grid-columns="2"
          />
        </div>
      </div>
    </div>

    <!-- Footer -->
    <div class="backpack-footer">
      <button class="return-button" @click="goHome">返回主页</button>
    </div>
  </div>
</template>

<script setup>
import { ref, inject } from 'vue';
import PokemonCard from './PokemonCard.vue';
import {typeColorMap} from "@/api/constants.js";

const navigate = inject('navigate');
const myPokemon = inject('myPokemon');
const items = inject('items');
const selectedPokemon = ref(null);
const activeTab = ref('pokemon');

const tabs = [
  { id: 'pokemon', name: '宝可梦' },
  { id: 'items', name: '物品' },
  { id: 'badges', name: '徽章' }
];

const goHome = () => {
  navigate('home');
};

const selectPokemon = (pokemon) => {
  selectedPokemon.value = pokemon;
};


const getTypeColor = (type) => {
  return typeColorMap[type] || '#9e9e9e';
};
</script>

<style scoped>
.backpack-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.backpack-header {
  background-color: #333;
  color: white;
  padding: 16px;
  display: flex;
  align-items: center;
}

.back-button {
  background: none;
  color: white;
  margin-right: 16px;
  padding: 8px;
}

.back-icon {
  font-size: 24px;
}

.header-title {
  font-size: 20px;
  font-weight: bold;
}

.backpack-content {
  flex: 1;
  padding: 16px;
}

.backpack-card {
  background-color: white;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  border-bottom: 1px solid #eee;
}

.tab-button {
  padding: 16px;
  background: none;
  border: none;
  font-size: 16px;
  color: #666;
  transition: all 0.3s;
}

.tab-button.active {
  color: #3498db;
  font-weight: bold;
  box-shadow: inset 0 -2px 0 #3498db;
}

.tab-content {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
}

.pokemon-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 1fr);
  gap: 16px;
}

.pokemon-card {
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.2s;
  overflow: hidden;
  user-select: none;
}

.pokemon-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.pokemon-card.selected {
  border: 2px solid #3498db;
}

.pokemon-card-content {
  padding: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.pokemon-thumbnail {
  width: 80px;
  height: 80px;
}

.pokemon-card-name {
  font-weight: 500;
  text-align: center;
  margin: 8px 0;
}

.type-badges {
  display: flex;
  gap: 4px;
  margin-top: 4px;
}

.type-badge {
  font-size: 12px;
  padding: 2px 8px;
  font-weight: 600;
  border-radius: 12px;
  color: white;
}

.pokemon-level {
  font-size: 12px;
  color: #666;
  margin-top: 4px;
}

.items-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 16px;
}

.item-card {
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.item-image {
  width: 48px;
  height: 48px;
  margin-bottom: 8px;
}

.item-name {
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 4px;
}

.item-quantity {
  font-size: 12px;
  color: #666;
}

.empty-tab {
  height: 300px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #999;
}

.info-icon {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background-color: #eee;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  margin-bottom: 8px;
  opacity: 0.5;
}

.backpack-footer {
  background-color: #333;
  padding: 16px;
}

.return-button {
  width: 100%;
  background-color: #3498db;
  color: white;
  padding: 12px;
  font-size: 16px;
}

.return-button:hover {
  background-color: #2980b9;
}
</style>